package gui;

import java.util.GregorianCalendar;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;

/**
 * @author sebastian, markus
 * 
 * Composite for simply change the actually date.
 *
 */
public class DateChangeComposite extends Composite {

	/**
	 * Calendarmode (Week, Month).
	 */
	private int mode;
	/**
	 * overview calendar for quickchange of the day, month, year, ...
	 */
	private DateTime dateTime;
	/**
	 * Button to select week, month before.
	 */
	private Label leftArrow;
	/**
	 * Button to select week, month after.
	 */
	private Label rightArrow;
	
	/*-----------------------------------------------------------------------------------*/

	/**
	 * @param parent parent control
	 * @param style style of new DateChangeComopsite (SWT-style)
	 */
	public DateChangeComposite(final Composite parent, final int style) {
		super(parent, style);
		FormLayout layout = new FormLayout();
		this.setLayout(layout);
		setMode(GregorianCalendar.WEEK_OF_MONTH);
		
		this.leftArrow = new Label(this, SWT.NONE);
		FormData data = new FormData();
		data.left = new FormAttachment(0, 2);
		data.top = new FormAttachment(0, 3);
		this.leftArrow.setLayoutData(data);
		this.leftArrow.setImage(new Image(this.getDisplay(), "./img/left-arrow.png"));
		this.leftArrow.addMouseListener(new MouseListener() {
			
			@Override
			public void mouseUp(final MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseDown(final MouseEvent arg0) {
				GregorianCalendar cal = getDate();
				cal.add(mode, -1);
				setDate(cal);
				notifyListeners(SWT.Modify, new Event());
			}
			
			@Override
			public void mouseDoubleClick(final MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		
		this.dateTime = new DateTime(this, SWT.DROP_DOWN | SWT.FLAT);
		data = new FormData();
		data.left = new FormAttachment(this.leftArrow, 3);
		data.top = new FormAttachment(0, 1);
		this.dateTime.setLayoutData(data);
		this.dateTime.addSelectionListener(new SelectionListener() {
			
			@Override
			public void widgetSelected(final SelectionEvent e) {
				notifyListeners(SWT.Modify, new Event());
			}
			
			@Override
			public void widgetDefaultSelected(final SelectionEvent e) {
				// TODO Auto-generated method stub
				
			}
		});
		
		this.rightArrow = new Label(this, SWT.NONE);
		data = new FormData();
		data.left = new FormAttachment(this.dateTime, 2);
		data.top = new FormAttachment(0, 3);
		this.rightArrow.setLayoutData(data);
		this.rightArrow.setImage(new Image(this.getDisplay(), "./img/right-arrow.png"));
		this.rightArrow.addMouseListener(new MouseListener() {
			
			@Override
			public void mouseUp(final MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseDown(final MouseEvent arg0) {
				GregorianCalendar cal = getDate();
				cal.add(mode, 1);
				setDate(cal);
				notifyListeners(SWT.Modify, new Event());
			}
			
			@Override
			public void mouseDoubleClick(final MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}
		});
	}

	/**
	 * @return the mode
	 */
	public int getMode() {
		return mode;
	}

	/**
	 * @param mode the mode to set
	 */
	public void setMode(final int mode) {
		this.mode = mode;
	}

	/**
	 * @param date this control will be set to date
	 */
	public void setDate(final GregorianCalendar date) {
		this.dateTime.setYear(date.get(GregorianCalendar.YEAR));
		this.dateTime.setMonth(date.get(GregorianCalendar.MONTH));
		this.dateTime.setDay(date.get(GregorianCalendar.DAY_OF_MONTH));
	}
	
	/**
	 * @return Gregoriancalender of this control
	 */
	public GregorianCalendar getDate() {
		return  new GregorianCalendar(this.dateTime.getYear(),
				this.dateTime.getMonth(),
				this.dateTime.getDay());
	}
}
